//app.js
import { getComponyTask, getRejectTask } from './driver_side/network/other.js'
import { getOrderReceipt } from './driver_side/network/order.js'
import { toast } from './utils/Tools'
App({
  onLaunch: function () {
    const login_status =  wx.getStorageSync('login_status');
    if(login_status === 'driver'){
      wx.reLaunch({
        url: '/driver_side/pages/order-hall/index/index'
      });
    }else{
      return;
    }
    wx.getSystemInfo({
      success: res => {
      let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          this.globalData.isIPhone = true
          return;
        }
        if (modelmes.search('iPhone') == -1 || modelmes.includes('iPhone 6/7/8') || modelmes.includes('iPhone 5') || modelmes.includes('iPad')) {
          return;
        }else{
          this.globalData.isIPhone = true
        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  onHide(){
    clearInterval(this.globalData.driverTask)
  },
  //      请求分配任务
  _task() {
    console.log('你他吗的在请求啥内容啊')
    getComponyTask().then(res=>{
      console.log(res)
     console.log('mmp了，你他妈的，我看见就烦了')
      // clearInterval(this.globalData.driverTask)
      if(res.code === 200){
        const {order_id} = res.data
        // clearTimeout(this.globalData.driverTask);
        // clearInterval(this.globalData.driverTask);
        wx.showModal({
          title: '任务分配',
          content: '是否接受公司任务分配',
          showCancel: true,
          success: async (result) => {
            if(result.confirm){
              const res = await getOrderReceipt(order_id)
              if(res.code === 200){
                toast('接单成功','success',200)
              }else{
                toast('接单失败','success',200)
              }
              // this.globalData.driverTask = setInterval(this._task,60000)
            }else{
              const err = await getRejectTask(order_id)
              console.log(err)
              if(err.code === 200){
                toast('拒绝分配','success',200)
                return;
              }
            }
          }
        });
      }else if(res.code===500){
        
        clearInterval(this.globalData.driverTask)
        wx.reLaunch({
          url: '/driver_side/pages/other/login/login'
        });
      }else{
        console.log('wocao,你在干嘛啊')
        // this.globalData.driverTask = setInterval(this._task,60000)
      }
    })    
  },
  globalData: {
    BASE_URL:'https://master.gddzxcx.com/',
    userInfo: null,
    selectCity: null,
    driver_user:null,
    business_user:null,
    isIPhone: false,
    driverTask:null,
    driverStatus:1,
    list: [{
      "text": "订单大厅",
      "iconPath": "/assets/images/product@2x.png",
      "selectedIconPath": "/assets/images/product-on@2x.png"
      },
      {
          "text": "订单进程",
          "iconPath": "/assets/images/order@2x.png",
          "selectedIconPath": "/assets/images/order-on@2x.png"
      },
      {
          "text": "业绩",
          "iconPath": "/assets/images/achievement@2x.png",
          "selectedIconPath": "/assets/images/achievement-on@2x.png"
      },
      {
          "text": "我的",
          "iconPath": "/assets/images/client@2x.png",
          "selectedIconPath": "/assets/images/client-on@2x.png"
    }]
  }
})